/**
 * 
 */
package com.asdan.seededu.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.asdan.seededu.entity.security.Principal;
import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.service.UserService;
import com.asdan.seededu.util.DatabaseAccess;


/**
 * @author lute
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	
	/* (non-Javadoc)
	 * @see com.demo2do.alaska.service.UserService#getPrincipal(java.lang.String)
	 */
	public Principal getPrincipal(String name) {
			
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			Principal pri = null;
			Connection cn = null;
			PreparedStatement pstmt=null;
			ResultSet rs = null;
			String sql = "select id , is_active , authority , name ,password from Principal  where name =? and is_active = 1 ";
			try{
				cn = databaseAccess.getConn();
				pstmt = cn.prepareStatement(sql);
				pstmt.setString(1, name);
				rs = pstmt.executeQuery();
				while(rs.next()){
					pri = new Principal();
					pri.setId(rs.getLong("ID"));
					pri.setAuthority(rs.getString("authority")) ;
					pri.setName(rs.getString("name")) ;
					pri.setPassword(rs.getString("password")) ;
				}
			}catch(Exception e){
				e.printStackTrace();
				System.out.println(e.getMessage());
			}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
				try{
					pstmt.close();
					cn.close();
					databaseAccess.close() ;
				}catch(Exception ex){
						pstmt = null ;
						cn = null ;
				}
			}
			return pri;
}

	public List<User> findUser(String name) {
		// TODO Auto-generated method stub
		
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		List<User> teaList = new ArrayList<User>()  ;
		String sql = "select   uid , name ,email ,studentorteacher  from user  where  ifdelete = 0 and name like '%"+name+"%' " ;
		try{
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql);
			System.out.println(sql );
			rs = pstmt.executeQuery();
			while(rs.next()){
				
				User tea= new User();
				tea.setUid(rs.getInt("UID")) ;
				tea.setName(rs.getString("NAME")) ;
				tea.setEmail(rs.getString("email")) ;
				tea.setStudentorteacher(rs.getInt("studentorteacher")) ;
				teaList.add(tea) ;
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			try{
				pstmt.close();
				cn.close();
				databaseAccess.close() ;
			}catch(Exception ex){
					pstmt = null ;
					cn = null ;
			}
		}
		return teaList ;
	}
	
}
